﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class SiteManagement : System.Web.UI.Page
    {
        protected SiteService siteService;
        protected ZoneService zoneService;

        protected void Page_Load(object sender, EventArgs e)
        {
            siteService = new SiteService(new SiteManager(), new ActiviteManager(), new CreneauService(new CreneauManager()));
            zoneService = new ZoneService(new ZoneManager());
            
        #region binding gridView

            List<SITE> sites = siteService.getAllSites();
            if (sites == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (sites.Count == 0)
            {
                lblError.Text = "No place in the database";
                gdwSites.DataBind();
            }
            else
            {
                lblError.Text = "";
                gdwSites.DataSource = sites;
                gdwSites.DataBind();
            }

            if (!IsPostBack)
            {
                ddlZone.DataSource = zoneService.getAllZones();
                ddlZone.DataTextField = "NOM";
                ddlZone.DataValueField = "ID";
                ddlZone.DataBind();
            }

            #endregion
        }   

        protected void gdwSites_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            double latitude = 0;
            double longitude = 0;
            double.TryParse(txtLatitude.Text.Replace('.', ','), out latitude);
            double.TryParse(txtLongitude.Text.Replace('.', ','), out longitude);
            lblError.Visible = true;
            lblError.Text = latitude.ToString();
            siteService.createSite(int.Parse(ddlZone.SelectedValue), txtNom.Text, latitude, longitude);


            Response.Redirect("SiteManagement.aspx");
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            double latitude = 0;
            double longitude = 0;
            double.TryParse(txtLatitude.Text.Replace('.', ','), out latitude);
            double.TryParse(txtLongitude.Text.Replace('.', ','), out longitude);

            siteService.updateSite(int.Parse(idUpdate.Value), txtNom.Text, latitude, longitude, int.Parse(ddlZone.SelectedValue));
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("SiteManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            txtNom.Text = "";
            txtLatitude.Text = "";
            txtLongitude.Text = "";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            SITE site = siteService.getSiteById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = site.NOM;
            txtLatitude.Text = "" + site.LATITUDE;
            txtLongitude.Text = "" + site.LONGITUDE;
            ddlZone.SelectedValue = "" + site.ZONEID;

            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            idUpdate.Value = "" + site.ID;

            hdfScrollIntoView.Value = "True";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            siteService.deleteSite(int.Parse(((ImageButton)sender).CommandArgument));

            Response.Redirect("SiteManagement.aspx");
        }

        protected void gdwSites_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwSites.DataSource = siteService.getAllSites();
            gdwSites.PageIndex = e.NewPageIndex;
            gdwSites.DataBind(); 
        }
    }
    
}